home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / rng / randu.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-05  |  2.5 KB  |  96 lines

  1. /* rng/randu.c
  2.  * 
  3.  * Copyright (C) 1996, 1997, 1998, 1999, 2000 James Theiler, Brian Gough
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or (at
  8.  * your option) any later version.
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #include <config.h>
  21. #include <stdlib.h>
  22. #include <gsl/gsl_rng.h>
  23.  
  24. /* This is a reincarnation of the infamously bad RANDU generator.
  25.    The sequence is,
  26.  
  27.    x_{n+1} = (a x_n) mod m
  28.  
  29.    with a = 65539 and m = 2^31 = 2147483648. The seed specifies
  30.    the initial value, x_1.
  31.  
  32.    The theoretical value of x_{10001} is 1623524161.
  33.  
  34.    The period of this generator is 2^29.
  35.  
  36.    Note: Knuth describes this generator as "really horrible". 
  37.  
  38.    From: Park and Miller, "Random Number Generators: Good ones are
  39.    hard to find" Communications of the ACM, October 1988, Volume 31,
  40.    No 10, pages 1192-1201. */
  41.  
  42. static inline unsigned long int randu_get (void *vstate);
  43. static double randu_get_double (void *vstate);
  44. static void randu_set (void *state, unsigned long int s);
  45.  
  46. static const long int a = 65539;
  47. static const unsigned long int m = 2147483648UL;
  48.  
  49. typedef struct
  50.   {
  51.     unsigned long int x;
  52.   }
  53. randu_state_t;
  54.  
  55. static inline unsigned long int
  56. randu_get (void *vstate)
  57. {
  58.   randu_state_t *state = (randu_state_t *) vstate;
  59.  
  60.   /* The following line relies on unsigned 32-bit arithmetic */
  61.  
  62.   state->x = (a * state->x) & 0x7fffffffUL;
  63.  
  64.   return state->x;
  65. }
  66.  
  67. static double
  68. randu_get_double (void *vstate)
  69. {
  70.   return randu_get (vstate) / 2147483648.0 ;
  71. }
  72.  
  73. static void
  74. randu_set (void *vstate, unsigned long int s)
  75. {
  76.   randu_state_t *state = (randu_state_t *) vstate;
  77.  
  78.   if (s == 0)
  79.     s = 1;    /* default seed is 1 */
  80.  
  81.   state->x = s;
  82.  
  83.   return;
  84. }
  85.  
  86. static const gsl_rng_type randu_type =
  87. {"randu",            /* name */
  88.  0x7fffffffUL,            /* RAND_MAX */
  89.  1,                   /* RAND_MIN */
  90.  sizeof (randu_state_t),
  91.  &randu_set,
  92.  &randu_get,
  93.  &randu_get_double};
  94.  
  95. const gsl_rng_type *gsl_rng_randu = &randu_type;
  96.